package top.glkj.hms.entity;

import lombok.Getter;
import lombok.Setter;
import top.glkj.hms.utils.Status;

import javax.persistence.*;
import java.util.List;
import java.util.Objects;

/**
 * 用户资源表
 *
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/11/1 0:35
 */
@Entity
@Table(name = "hms_resource")
@Getter
@Setter
public class Resource extends BaseEntity {
    /**
     * 资源名称
     */
    @Column(name = "resource_name", nullable = false)
    private String resourceName;
    /**
     * 资源路径，完整路径为父级加子级
     */
    @Column(name = "resource_url")
    private String resourceUrl;
    /**
     * 资源类型，例如菜单，按钮，页面，图片，其他资源等
     */
    @Column(name = "resource_type", nullable = false)
    private String resourceType;
    /**
     * 请求方法，Get，Post,Put,Delete,
     */
    @Column(name = "resource_method", nullable = false)
    private String resourceMethod;
    /**
     * 父级资源
     */
    @OneToOne
    @JoinColumn(name = "resource_parent_id", referencedColumnName = "id")
    private Resource parent;

    @Transient
    private List<Resource> children;

    /**
     * 状态码
     */
    private Status status;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Resource)) {
            return false;
        }
        Resource resource = (Resource) o;
        return Objects.equals(getResourceName(), resource.getResourceName())
                && Objects.equals(getResourceUrl(), resource.getResourceUrl())
                && Objects.equals(getResourceType(), resource.getResourceType())
                && Objects.equals(getResourceMethod(), resource.getResourceMethod())
                && Objects.equals(getCreateUserId(), resource.getCreateUserId())
                && Objects.equals(getParent(), resource.getParent())
                && getStatus() == resource.getStatus();
    }

    @Override
    public int hashCode() {
        return Objects.hash(getResourceName(),
                getResourceUrl(),
                getResourceType(),
                getResourceMethod(),
                getCreateUserId(),
                getParent(),
                getStatus());
    }
}
